package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.oa.WorkSummaryType;
import com.prs.crm.service.oa.WorkSummaryTypeService;
import com.prs.crm.service.sys.LogService;

public class WorkSummaryTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private WorkSummaryType workSummaryType;
	private WorkSummaryTypeService workSummaryTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listWorkSummaryType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkSummaryType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWorkSummaryTypeService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}

	@Action(value = "loadWorkSummaryTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadWorkSummaryTypeTree() {
		nodes = new ArrayList<TreeNode>();
		List<WorkSummaryType> wts = this.getWorkSummaryTypeService()
				.getAllWorkSummaryType();
		for (WorkSummaryType wt : wts) {
			TreeNode node = new TreeNode();
			node.setId(wt.getId().toString());
			node.setText(wt.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}

	@Action("editWorkSummaryType")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveWorkSummaryType", results = {
			@Result(name = "success", type = "redirect", location = "listWorkSummaryType"),
			@Result(name = "error", type = "chain", location = "editWorkSummaryType") })
	public String save() {
		if (workSummaryType.getName() == null
				|| "".equals(workSummaryType.getName().trim())) {
			addActionError("类型名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(WorkSummaryType.class,
				workSummaryType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getWorkSummaryTypeService().save(workSummaryType);
		return SUCCESS;
	}

	@Action(value = "removeWorkSummaryType", results = {
			@Result(name = "success", type = "redirect", location = "listWorkSummaryType"),
			@Result(name = "error", type = "chain", location = "listWorkSummaryType") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择哟删除的工作总结类型");
			return ERROR;
		}
		for (Integer id : ids) {
			WorkSummaryType type = this.getWorkSummaryTypeService().get(id);
			if (type.getWorkSummarys().size() != 0) {
				this.addActionError("已有工作总结引用，不能删除");
				return ERROR;
			}
		}
		this.getWorkSummaryTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public WorkSummaryType getWorkSummaryType() {
		return workSummaryType;
	}

	public void setWorkSummaryType(WorkSummaryType workSummaryType) {
		this.workSummaryType = workSummaryType;
	}

	public WorkSummaryTypeService getWorkSummaryTypeService() {
		return workSummaryTypeService;
	}

	public void setWorkSummaryTypeService(
			WorkSummaryTypeService workSummaryTypeService) {
		this.workSummaryTypeService = workSummaryTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
